package yawning.mew.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;

import yawning.mew.character.ArmoryToonBuilder.ArmoryToonBuildException;
import yawning.mew.equipment.DatabaseItemSource;
import yawning.mew.equipment.ItemSource;
import yawning.mew.equipment.MemoryCacheItemSource;
import yawning.mew.equipment.WowheadItemSource;
import yawning.mew.gui.characters.ProfileContainer;
import yawning.mew.gui.listener.BattlenetHistoryListener;
import yawning.mew.gui.listener.IMainWindowListener;
import yawning.mew.gui.listener.MouseClickListener;
import yawning.mew.gui.tools.BattleNetCharacter;
import yawning.mew.gui.tools.CharacterDataSource;

public class MainWindow implements IMainWindowListener{

	private JFrame frmMew;
	JTabbedPane tabbedPane;
	private ArrayList<BattleNetCharacter> bnetHistory;
	private JMenu mnBattlenetHistory;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					 ItemSource.pushBack(new MemoryCacheItemSource());
					 ItemSource.pushBack(new DatabaseItemSource());
					 ItemSource.pushBack(new WowheadItemSource());
					MainWindow window = new MainWindow();
					window.frmMew.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWindow() {
		bnetHistory = new ArrayList<BattleNetCharacter>();
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmMew = new JFrame();
		frmMew.setTitle("Mew");
		frmMew.setResizable(false);
		frmMew.setBounds(100, 100, 800, 660);
		frmMew.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menu = new JMenuBar();
		frmMew.setJMenuBar(menu);
		
		JMenu mnFiles = new JMenu("Files");
		menu.add(mnFiles);
		
		JMenuItem mntmNew = new JMenuItem("New");
		mntmNew.addMouseListener(new MouseClickListener(this));
		mnFiles.add(mntmNew);
		
		JMenuItem mntmOpenFile = new JMenuItem("Open File");
		mnFiles.add(mntmOpenFile);
		
		mnBattlenetHistory = new JMenu("BattleNet History");
		mnBattlenetHistory.addMenuListener(new BattlenetHistoryListener(this));
		mnFiles.add(mnBattlenetHistory);
		
		JMenuItem mntmFilesSeparator = new JMenuItem("------------------");
		mnFiles.add(mntmFilesSeparator);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		mnFiles.add(mntmExit);
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		frmMew.getContentPane().add(tabbedPane, BorderLayout.CENTER);
		
		
/*		CharacterDataSource source;
		String name = "Ceridan";
		String server = "Greymane";
		String region = "us";
		try {
			source = new CharacterDataSource(name, server,region);
		} catch (ArmoryToonBuildException e) {
			e.printStackTrace();
			source = new CharacterDataSource(); //Temporary.
		}
		
		ProfileContainer profileContainer = new ProfileContainer(source);
		tabbedPane.addTab(name+"@"+server+"["+region+"]", null, profileContainer, null);*/
	}
	
	public void newCharacter(String name, String server, String region)
	{
		CharacterDataSource source;
		try {
			source = new CharacterDataSource(name, server,region);
		} catch (ArmoryToonBuildException e) {
			e.printStackTrace();
			source = new CharacterDataSource();
		}
		ProfileContainer profileContainer = new ProfileContainer(source);
		tabbedPane.addTab(name+"@"+server+"["+region+"]", null, profileContainer, null);
	}
	public void newCharacter()
	{
		CharacterDataSource source;
		source = new CharacterDataSource();
		ProfileContainer profileContaier = new ProfileContainer(source);
		tabbedPane.addTab("New Character", null,profileContaier,null);
	}

	@Override
	public void refreshMenu() {
		if(mnBattlenetHistory.getItemCount()>0){
			mnBattlenetHistory.removeAll();
		}
		if(!bnetHistory.isEmpty())
		{
			for (BattleNetCharacter character : bnetHistory) {
				JMenuItem mniHistory = new JMenuItem(character.getMenuItemText());
				
				mnBattlenetHistory.add(mniHistory);
			}
		}
	}

}
